import store from '@/store'

export default {
	ajaxLog(apiname,menu,user,domain,session,call_starttime,call_apitime,ui_outtime,message,timestamp){
		console.log(store.getters)
		try{
			uni.request({
				url:  store.getters.getAjaxLog,
				method: "POST",
				data: { 
					apiname: apiname,
					menu: menu,
					user: user,
					domain:domain,
					session: session,
					call_starttime: call_starttime,
					call_apitime: call_apitime,
					ui_outtime: ui_outtime,
					message: message,
					timestamp: timestamp.toString()
				},
				dataType: "json",
				sslVerify: false, //	是否验证ssl证书
				success: res => {
					console.log(res.data)
					return
				}
			})
		}catch(e){
			//TODO handle the exception
		}
	},
	beep(){
		try{
			plus.device.beep();
		}catch(e){
			console.log(e)
			//TODO handle the exception
		}
	},
	getCurrentTime() {
		var date = new Date();//当前时间
		var year = date.getFullYear() //返回指定日期的年份
		var month = this.repair(date.getMonth() + 1);//月
		var day = this.repair(date.getDate());//日
		var hour = this.repair(date.getHours());//时
		var minute = this.repair(date.getMinutes());//分
		var second = this.repair(date.getSeconds());//秒
		var millisecond = this.repair2(date.getMilliseconds());//毫秒
		
		//当前时间 
		var curTime = year + "-" + month + "-" + day
				+ " " + hour + ":" + minute + ":" + second + "." + millisecond;
		return curTime;
	},
	chkUrgentStockRequest(){
		try{
			uni.request({
				url:  store.getters.getAll.serviceUrl+'/RemainFeed',
				method: "POST",
				data: { 
					corp: store.getters.getCorp || '',
					domain: store.getters.getDomain || ''
				},
				dataType: "json",
				header: {
					'accept':'application/json',
					'Content-Type':'application/json',
				},
				sslVerify: false, //	是否验证ssl证书
				success: res => {
					var rtdt = JSON.parse(e.d);
					//				console.log('是否存在紧急备料需求 :' + JSON.stringify(rtdt));
					if(rtdt.v_remain.toLowerCase() == 'true') {
						return '存在紧急备料需求，请尽快处理。'
					} else {
						return ''
					}
				}
			})
		}catch(e){
			return
		}
	},
	//补0
	repair(i){
		if (i >= 0 && i <= 9) {
			return "0" + i;
		} else {
			return i;
		}
	},
	//补0
	repair2(i){
		if (i >= 0 && i <= 9) {
			return "00"+ i;
		} else if(i > 9 && i <= 99){
			return "0"+i;
		}else{
			return i
		}
	},
	logout() {
		uni.navigateTo({
			url:'/pages/login/login'
		})
	},
	imageCompress(file){
	    return new Promise((resolve, reject)=>{
	        let { size,path } = file
	        let type  = path.split(".")[1]
	        console.log(51,size,path,type)
	        //大于100kb进行压缩
	        if(size <= 200*1024){
	            resolve(file)
	            console.log(56,file)
	            return false
	        } 
	        uni.compressImage({
	            src: path,
	            quality: 80,
	            width:'50%',
	            height: '50%',
	            success: res => {
	                console.log(62,res)
					let newPath = ''
					if(res.tempFilePath.indexOf('.') === -1){
						 newPath = res.tempFilePath+type
					}else{
						 newPath = res.tempFilePath
					}
	                console.log(622,newPath)
	                uni.getFileInfo({
	                    filePath:res.tempFilePath,
	                    success:async (info)=>{
	                        console.log(73,info)
	                        let newFile = {...file,size:info.size,path:newPath,tempFilePath:res.tempFilePath}
							
	                        resolve(await this.imageCompress(newFile))
	                    }
	                })
	            },
	            fail:err=>{
	                console.log(63,err)
	            }
	        })    
	    })
	}
}
